package cn.swust.designpatternlearningplatform.service.impl;

import cn.swust.designpatternlearningplatform.dao.TopicDao;
import cn.swust.designpatternlearningplatform.dao.TopicKnowledgePointDao;
import cn.swust.designpatternlearningplatform.dao.TopicUpdateDao;
import cn.swust.designpatternlearningplatform.domain.dto.DifficultyDto;
import cn.swust.designpatternlearningplatform.domain.dto.KnowledgePointDto;
import cn.swust.designpatternlearningplatform.domain.entity.Topic;
import cn.swust.designpatternlearningplatform.domain.entity.TopicUpdate;
import cn.swust.designpatternlearningplatform.domain.enums.TopicDiffEnum;
import cn.swust.designpatternlearningplatform.service.TopicUpdateService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TopicUpdateServiceImpl implements TopicUpdateService {
    @Resource
    private TopicKnowledgePointDao topicKnowledgePointDao;
    @Resource
    private TopicUpdateDao topicUpdateDao;
    @Resource
    private TopicDao topicDao;

    /**
     * 获取学生的题型的知识点分布
     *
     * @param studentNumber
     * @param updateType
     * @param topicType
     * @return java.util.List<cn.swust.designpatternlearningplatform.domain.dto.KnowledgePointDto>
     * @author Tang
     * @date 2023/7/18 14:07
     **/
    @Override
    public List<KnowledgePointDto> getKnowledgePointInTopicType(String studentNumber, String updateType, String topicType) {
        List<Integer> topicIds = getTopicIds(studentNumber, updateType, topicType);
        if (topicIds.isEmpty()) {
            return new ArrayList<>();
        }
        //查询并包装知识点信息
        return topicKnowledgePointDao.selectByTopicIds(topicIds).stream().map(i -> {
            KnowledgePointDto knowledgePointDto = new KnowledgePointDto();
            BeanUtils.copyProperties(i, knowledgePointDto);
            return knowledgePointDto;
        }).collect(Collectors.toList());
    }


    /**
     * 获取学生的题目难度分布
     *
     * @param studentNumber
     * @param updateType
     * @param topicType
     * @return cn.swust.designpatternlearningplatform.domain.dto.DifficultyDto
     * @author Tang
     * @date 2023/7/18 14:27
     **/
    @Override
    public DifficultyDto getDifficultyDistribution(String studentNumber, String updateType, String topicType) {
        List<Integer> topicIds = getTopicIds(studentNumber, updateType, topicType);
        if(topicIds.isEmpty()) {
            return new DifficultyDto();
        }
        List<String> collect = topicDao.selectTopicsByIds(topicIds)
                .stream()
                .map(Topic::getTopicDifficulty)
                .collect(Collectors.toList());

        DifficultyDto difficultyDto = new DifficultyDto();
        int easy = 0, middle = 0, hard = 0;
        for (String i : collect) {
            if (TopicDiffEnum.EASY.typeName.equals(i)) {
                easy++;
            }
            if (TopicDiffEnum.ORDINARY.typeName.equals(i)) {
                middle++;
            }
            if (TopicDiffEnum.DIFFICULTY.typeName.equals(i)) {
                hard++;
            }
        }
        difficultyDto.setEasy(easy);
        difficultyDto.setMiddle(middle);
        difficultyDto.setHard(hard);
        return difficultyDto;
    }

    /**
     * 获取学生的题目id
     *
     * @param studentNumber
     * @param updateType
     * @param topicType
     * @return java.util.List<java.lang.Integer>
     * @author Tang
     * @date 2023/7/18 14:29
     **/
    @NotNull
    private List<Integer> getTopicIds(String studentNumber, String updateType, String topicType) {
        TopicUpdate topicUpdate = new TopicUpdate();
        topicUpdate.setStudentNumber(studentNumber);
        topicUpdate.setUpdateType(updateType);
        topicUpdate.setTopicType(topicType);
        //获取所有的题目id
        Integer total = topicUpdateDao.selectTopicUpdateByStudentTotal(topicUpdate);
        if(total <= 0) {
            return new ArrayList<>();
        }
        return topicUpdateDao.
                selectTopicUpdateByStudent(topicUpdate, 0, total)
                .stream()
                .map(TopicUpdate::getTopicId).collect(Collectors.toList());
    }
}
